﻿using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.Domains.ReturnNotice;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;

namespace SalesManagementCommon.API.SalesManagementCommons.ReturnNotices
{
    public class ReturnNoticeQueryHander:IRequestHandler<ReturnNoticeQueryCommon,Result<PageResultDto<ReturnNoticesDTO>>>
    {
        private readonly ISqlSugarClient _sqlSugarClient;

        public ReturnNoticeQueryHander(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 查询退货通知单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<ReturnNoticesDTO>>> Handle(ReturnNoticeQueryCommon request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<ReturnNoticesDTO>>();
            var page = new PageResultDto<ReturnNoticesDTO>() { TotalCount = 0, TotalPage = 0, List = new List<ReturnNoticesDTO>() };
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var list = _sqlSugarClient.Queryable<refundNotice, refundNoticeMaterial>((a, b) => a.ThSerialnumber == b.ThSerialnumber).Select((a, b) => new ReturnNoticesDTO()
                {
                    ReturnNoticesId = a.ReturnNoticesId,
                    Documentnumber = b.Documentnumber,// 销售订单号
                    ReturnNumber = a.ReturnNumber,//退票通知单单据编号
                    Documentstatus = a.Documentstatus,// 单据状态
                    ClientName = a.ClientName,// 客户名称
                    MaterialName = b.MaterialName,// 物料名称
                    ItemBianma = b.ItemBianma,// 物料编码
                    PiHao = b.PiHao,// 批号
                    ReturnShulnum = b.ReturnShulnum,// 退货数量
                    BeginNumbere = b.BeginNumbere,// 入库数量
                    DocumentsDate = a.DocumentsDate,// 单据日期

                }).MergeTable();
                if (!string.IsNullOrEmpty(request.Documentnumber))// 销售订单号
                {
                    list = list.Where(a => a.Documentnumber.Contains(request.Documentnumber));
                }
                if (!string.IsNullOrEmpty(request.ReturnNumber))//退票通知单单据编号
                {
                    list = list.Where(a => a.ReturnNumber.Contains(request.ReturnNumber));
                }
                if (!string.IsNullOrEmpty(request.ClientName))// 客户名称
                {
                    list = list.Where(a => a.ClientName.Contains(request.ClientName));
                }
                if (!string.IsNullOrEmpty(request.SalesStaff))// // 销售人员
                {
                    list = list.Where(a => a.ClientName.Contains(request.SalesStaff));
                }
                if (!string.IsNullOrEmpty(request.MaterialName))// // 物料名称
                {
                    list = list.Where(a => a.ClientName.Contains(request.MaterialName));
                }
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取退货通知单成功";
                }
                else
                {
                    result.Data = new PageResultDto<ReturnNoticesDTO>() { List = new List<ReturnNoticesDTO>() };
                    result.Msg = "没有查询到退货通知单";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
